package recharge_management

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/xerrors/assets"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type RechargeVipLevelUpdateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// VIP充值等级--修改
func NewRechargeVipLevelUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RechargeVipLevelUpdateLogic {
	return &RechargeVipLevelUpdateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RechargeVipLevelUpdateLogic) RechargeVipLevelUpdate(req *types.RechargeVipLevelUpdateRequest) (resp *types.CommonResponse, err error) {

	if len(req.Records) == 0 {
		return &types.CommonResponse{
			CommonResp: types.CommonResp{
				Code:    assets.ParameterError,
				Message: "not enough parameter",
			},
		}, nil
	}

	records := make([]*rpc.VipLevel, len(req.Records))
	for i, r := range req.Records {
		records[i] = &rpc.VipLevel{
			Id:     r.Id,
			Level:  r.Level,
			Times:  r.Times,
			Amount: r.Amount,
		}
	}

	rpcReq := rpc.RechargeVipLevelUpdateRequest{
		Records: records,
	}

	rpcResp, err := l.svcCtx.AssetsRpc.PayRecharge.RechargeVipLevelUpdate(l.ctx, &rpcReq)
	if err != nil {
		return nil, err
	}

	return &types.CommonResponse{
		CommonResp: types.CommonResp{
			Code:    rpcResp.CommonResp.Code,
			Message: rpcResp.CommonResp.Message,
		},
	}, nil
}
